package com.smartstate.dataManagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.dataManagement.dto.*;
import com.smartstate.dataManagement.service.EquipmentConfigService;
import com.smartstate.dataManagement.vo.DeviceAlarmVO;
import com.smartstate.dataManagement.vo.DeviceAttributeVO;
import com.smartstate.dataManagement.vo.ResultVO;
import io.swagger.annotations.ApiOperation;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/EquipmentConfig")
public class EquipmentConfigController {

    @Resource
    private EquipmentConfigService equipmentConfigService;

    @PostMapping("/addDeviceAttribute")
    @ApiOperation(value = "新增设备属性")
    public ResultVO<Integer> addDeviceAttribute(@RequestBody DeviceAttributeDTO deviceAttributeDTO) {
        return ResultVO.ok(equipmentConfigService.addDeviceAttribute(deviceAttributeDTO));
    }

    @DeleteMapping("/deleteDeviceAttribute")
    @ApiOperation(value = "删除设备属性")
    public ResultVO<Integer> removeById(@RequestParam("deviceId") String deviceId, @RequestParam("attributeAlias") String attributeAlias) {
        return ResultVO.ok(equipmentConfigService.deleteDeviceAttribute(deviceId, attributeAlias));
    }

    /**
     * 点击设备的属性配置获取设备属性分页
     *
     * @param pageDTO
     * @return
     */
    @PostMapping("/deviceAttribute")
    @ApiOperation(value = "点击设备的属性配置获取设备属性分页")
    public Page<DeviceAttributeVO> deviceAttribute(@RequestBody PageDTO<EquipmentDTO> pageDTO) {
        return equipmentConfigService.deviceAttribute(pageDTO);
    }


    @PutMapping("/updateDeviceAttribute")
    @ApiOperation(value = "编辑设备属性")
    public ResultVO<Integer> editDeviceAttribute(@RequestBody AttributeDTO attributeDTO) {
        return ResultVO.ok(equipmentConfigService.updateDeviceAttribute(attributeDTO));
    }


    @PostMapping("/deviceAlarmPage")
    @ApiOperation(value = "设备报警分页")
    public Page<DeviceAlarmVO> deviceAlarmPage(@RequestBody PageDTO<DeviceAlarmDTO> pageDTO ) {
        return equipmentConfigService.deviceAlarmPage(pageDTO);
    }

    @PutMapping("/deviceAlarmEdit")
    @ApiOperation(value = "设备报警编辑")
    public ResultVO<Integer> deviceAlarmEdit(@RequestBody DeviceAlarmDTO deviceAlarmDTO ) throws MqttException {
        return ResultVO.ok(equipmentConfigService.deviceAlarmEdit(deviceAlarmDTO));
    }


    @PutMapping("/enabledAttribute")
    @ApiOperation(value = "设备属性启用禁用")
    public ResultVO<Integer> enabledAttribute(@RequestBody AttributeDTO attributeDTO ) {
        return ResultVO.ok(equipmentConfigService.enabledAttribute(attributeDTO),attributeDTO.getEnabled() ? "启用成功" : "禁用成功");
    }
}
